package com.jereh.fuckmylife.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.jereh.fuckmylife.R;
import com.jereh.fuckmylife.activity.HotCatStandardActivity;
import com.jereh.fuckmylife.activity.LiveWeekChartsActivity;
import com.jereh.fuckmylife.adapter.InfoPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播界面
 * Created by BKSmy on 2017/1/4.
 */
public class LiveFragment extends Fragment {
    private Intent intent;
    private Context context;
    private View view;
    private RadioGroup radioGroup;
    private ImageView week;
    private ViewPager viewPager;
    private PullToRefreshGridView pullGridView;
    private GridView gridView;
    private InfoPagerAdapter pagerAdapter;
    private List<ImageView> imageViewList=new ArrayList<>();
    private int position;

    @Override
    public void onAttach(Context context) {
        this.context=context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_live,null,false);
        setView();
        Listener();
        return view;
    }

    private void setView() {
        radioGroup=(RadioGroup)view.findViewById(R.id.radioGroup);
        week=(ImageView)view.findViewById(R.id.week);
        viewPager=(ViewPager)view.findViewById(R.id.viewPager);
        pullGridView=(PullToRefreshGridView)view.findViewById(R.id.pullGridView);
        gridView=pullGridView.getRefreshableView();
        radioGroup.check(0);
        ((RadioButton)radioGroup.getChildAt(0)).setTextSize(R.dimen.text_size_21);
        gridView.setNumColumns(2);
        intent=new Intent();
        setAdapter();
    }

    private void setAdapter() {
        setPagerAdapter();
    }

    private void setPagerAdapter() {
        int[] images=new int[]{R.mipmap.tu1,R.mipmap.tu2};
        for(int i=0;i<2;i++){
            ImageView imageView=new ImageView(context);
            imageView.setImageResource(images[i]);
            imageViewList.add(imageView);
        }
        pagerAdapter=new InfoPagerAdapter(imageViewList);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);
    }

    private void Listener(){
        radioGroupListener();
        weekChartsListener();
        viewPagerListener();
    }

    private void viewPagerListener() {
        intent.setClass(context, HotCatStandardActivity.class);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("position",getPosition());
                startActivity(intent);
            }
        });
    }

    private void weekChartsListener() {
        intent.setClass(context, LiveWeekChartsActivity.class);
        week.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }


    private void radioGroupListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    ((RadioButton) radioGroup.getChildAt(1)).setTextSize(R.dimen.text_size_18);
                }
                ((RadioButton) view.findViewById(checkedId)).setTextSize(R.dimen.text_size_21);
            }
        });
    }

    private void setPosition(int position){
        this.position=position;
    }

    private int getPosition(){
        return position;
    }
}
